package com.hgb.mall.admin.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.admin.dto.PmsBrandParam;
import com.hgb.mall.admin.services.PmsBrandService;
import com.hgb.mall.mbg.entity.PmsBrand;
import com.hgb.mall.mbg.mapper.PmsBrandDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.PmsBrandMapper;
import com.hgb.mall.mbg.mapper.PmsProductDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.PmsProductMapper;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * 商品品牌Service实现类
 * Created by hgb on 2020/4/26.
 */
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public List<PmsBrand> listAllBrand() {
        return brandMapper.select(c -> c);
    }

    @Override
    public int createBrand(PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return brandMapper.insertSelective(pmsBrand);
    }

    @Override
    public int updateBrand(Long id, PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        UpdateStatementProvider updateStatement = SqlBuilder.update(PmsProductDynamicSqlSupport.pmsProduct)
                                                            .set(PmsProductDynamicSqlSupport.brandName)
                                                            .equalTo(pmsBrand.getName())
                                                            .where(PmsProductDynamicSqlSupport.brandId, isEqualTo(id))
                                                            .build()
                                                            .render(RenderingStrategies.MYBATIS3);
        productMapper.update(updateStatement);
        return brandMapper.updateByPrimaryKeySelective(pmsBrand);
    }

    @Override
    public int deleteBrand(Long id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBrand(List<Long> ids) {
        return brandMapper.delete(c -> c.where(PmsBrandDynamicSqlSupport.id, isIn(ids)));
    }

    @Override
    public List<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return brandMapper.select(c -> c.where(PmsBrandDynamicSqlSupport.name, isLikeWhenPresent(keyword)
                .then(s -> "%" + s + "%"))
                                        .orderBy(PmsBrandDynamicSqlSupport.sort.descending()));
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return brandMapper.selectByPrimaryKey(id).orElse(null);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        return brandMapper.update(s -> s.set(PmsBrandDynamicSqlSupport.showStatus).equalTo(showStatus)
                                        .where(PmsBrandDynamicSqlSupport.id, isIn(ids)));
    }

    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        return brandMapper.update(c -> c.set(PmsBrandDynamicSqlSupport.factoryStatus).equalTo(factoryStatus)
                                        .where(PmsBrandDynamicSqlSupport.id, isIn(ids)));
    }
}
